﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Engine.NetClasses
{
    /// <summary>
    /// This class is designed for use as the object to be assigned to the SocketAsyncEventArgs.UserToken property. 
    /// </summary>
    class AsyncUserToken
    {

        Socket m_socket;
        private uint sesionID;
        private object locker;
        public object Locker
        {
            get
            {
                return locker;
            }
        }
        private ulong sessionKey;
        private uint frist;
        private uint sec;
        List<PacketsBilder> fromServer = new List<PacketsBilder>(0);
        List<PacketsBilder> fromClient = new List<PacketsBilder>(0);
        public PacketsBilder[] GetFromClient()
        {

            PacketsBilder[] tmp;
                lock (fromClient)
                {
                    tmp = fromClient.ToArray();
                    fromClient.Clear();
                }
                return tmp;
                      
        }
        public void SetFromClient(PacketsBilder FromClient)
        {

            
            lock (fromClient)
            {

                fromClient.Add(FromClient);
            }
           

        }
        public PacketsBilder[] GetFromServer()
        {

            PacketsBilder[] tmp;
            lock (fromClient)
            {
                tmp = fromServer.ToArray();
                fromServer.Clear();
            }
            return tmp;

        }
        public void SetFromServer(PacketsBilder FromServer)
        {


            lock (fromClient)
            {

                fromServer.Add(FromServer);
            }


        }
        public AsyncUserToken() : this(null,0) { }
        public AsyncUserToken(Socket socket,uint id)
        {
            m_socket = socket;
            sesionID = id;
        }
        public uint Id
        {
            get { return sesionID; }
            set { sesionID = value; }
        }
        public Socket Socket
        {
            get { return m_socket; }
            set { m_socket = value; }
        }
        public void DoPack()
        {
            PacketsBilder[] p = GetFromClient();
            for (int i = 0; i < p.Length; i++)
            {
                switch (p[i].Header._ptype)
                {
                    case 3:
                        
                       string login=new string(((RequestLoginAuth)p[i].getStryct()).Login);
                         string pass=new string(((RequestLoginAuth)p[i].getStryct()).Password);
                        if (
                            login.Trim() != "admin"
                            &&
                           pass.Trim() != "admin"
                            )
                        {


                            LoginFail l = (LoginFail)NetPacket.GetPacket(4);
                            l.FailCode = 0;
                            SetFromServer(new PacketsBilder(l.GetByte(), 0));
                        }
                        else
                        {
                            sessionKey=(sesionID+((ulong)Environment.TickCount/5632))*256/65*659/45-1632+8456*2;
                            byte[] tmp=BitConverter.GetBytes(sessionKey);
                        frist=BitConverter.ToUInt32(tmp,0);
                        sec=BitConverter.ToUInt32(tmp,4);
                        LoginOk l = (LoginOk)NetPacket.GetPacket(2);
                            l.Frist = frist;
                            l.Secondory = sec;
                            SetFromServer(new PacketsBilder(l.GetByte(), 0));
                        }
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    default:
                        break;
                }
            }

        }

    }
}
